<?php
error_reporting(E_ERROR);
header("Content-type: text/html;charset=utf-8");
if (file_exists('../leecms.lock')) {
    exit('该程序已经安装过了。如您要重新安装，请先删除leecms.lock文件。');
}
$basepath = substr(dirname(__FILE__), 0, strrpos(dirname(__FILE__), "\\"));
$basepath = substr($basepath, strrpos($basepath, "\\") + 1);
$step = isset($_GET["step"]) ? $_GET["step"] : "";
if (empty($step)) {
    $step = "step1";
}

if ($step == "install") {
    crate_db_config();
    install_data((int)$_POST["isTestData"]);
    touch($_SERVER["DOCUMENT_ROOT"] . "/leecms.lock");
} else {
    ob_start();
    require($_SERVER['DOCUMENT_ROOT'] . "/install/$step.php");
    $content = ob_get_contents();
    ob_end_clean();
    echo $content;
}


function crate_db_config()
{
    $path = $_SERVER["DOCUMENT_ROOT"] . "/config/dbconfig.php";
    $dbhost = $_POST["dbhost"];
    $dbport = $_POST["dbport"];
    $dbname = $_POST["dbname"];
    $dbuser = $_POST["dbuser"];
    $dbpass = $_POST["dbpass"];
    $charset = isset($_POST["charset"]) ? $_POST["charset"] : "utf8";
    $dbprefix = isset($_POST["dbprefix"]) ? $_POST["dbprefix"] : "lee_";
    $content = '<?php
    $dbinfo = array();
    //数据库地址
    $dbinfo["dbhost"] = "' . $dbhost . '";
    //数据库端口
    $dbinfo["dbport"] = ' . $dbport . ';
    //数据库名称
    $dbinfo["dbname"] =  "' . $dbname . '";
    //数据库账号
    $dbinfo["dbuser"] =  "' . $dbuser . '";
    //数据库密码
    $dbinfo["dbpass"] = "' . $dbpass . '";
    //数据库字符集
    $dbinfo["charset"] = "' . $charset . '";
    //表前缀
    $dbinfo["dbprefix"] = "' . $dbprefix . '";
    ?>';
    $hand = fopen($path, "w");
    fwrite($hand, $content);
    fclose($hand);
}

/**
 * 还原指定sql脚本
 * @param $bkname
 * @param int $type 1 安装表结构 2 安装表结构和数据
 */
function install_data($type = 1)
{
    if (!$type) {
        die("未知安装类型,请勿非法访问");
    }
    include $_SERVER['DOCUMENT_ROOT'] . "/config/dbconfig.php";
    $conn = mysqli_connect($dbinfo["dbhost"], $dbinfo["dbuser"], $dbinfo["dbpass"], $dbinfo["dbname"], $dbinfo["dbport"]);
    if (!$conn) {
        die("连接数据库失败,请检查配置信息是否正确");
    }
    $conn->set_charset($dbinfo["charset"]);
    $name = $_SERVER["DOCUMENT_ROOT"] . "/install/dbscript/table.sql";
    $ftable = fopen($name, "r") or die("打开表结构文件失败");
    $table_data = fread($ftable, filesize($name));
    $tarr = explode(";", $table_data);
    foreach ($tarr as $key => $val) {
        $temp = str_replace("lee_", $dbinfo["dbprefix"], $val);
//       error_log($temp);
        $conn->query($temp);
    }
    fclose($ftable);
    if ($type === 2) {
        $dname = $_SERVER["DOCUMENT_ROOT"] . "/install/dbscript/data.sql";
        $fdata = fopen($dname, "r") or die("打开数据文件失败");
        $data = fread($fdata, filesize($dname));
        $dataarr = explode(";-- split line---", $data);
        foreach ($dataarr as $key => $val) {
            $temp = str_replace("lee_", $dbinfo["dbprefix"], $val);
//            error_log($temp);
            $conn->query($temp);
        }
        fclose($fdata);
    }
    //将用户创建的账号信息插入数据库
    $sql = "INSERT INTO " . $dbinfo["dbprefix"] . "admin(rolesinfo_single_id, user_name, 
                password, email, webchat, phone, tel, created_by, 
                created_date, updated_by, updated_date, version, 
                is_del, status_code, status_name, local) 
       VALUES ( 0, '" . $_POST["username"] . "', '" . md5($_POST[password]) . "', '" . $_POST["email"] . "',
        '', '" . $_POST["phone"] . "', '', 'NoLogin', now(), 'NoLogin', now(), 1, 0, '1', '正常', 'zh_CN')";
    $conn->query($sql);

    $conn->close();
    echo "ok";
}

?>
